<template>
  <el-dialog v-if="load" title="手动添加快递单号" v-model="visible" append-to-body :close-on-click-modal="false" width="800px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <div class="grid grid-cols-3 gap-x-3">
        <template v-for="(d, i) in form.data" :key="i">
          <el-form-item label="快递单号" :prop="['data', i, 'tracking_no']" :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
            <el-input v-model="d.tracking_no" placeholder="请输入" class="w-full" @change="val => getSupplier(val, d)" />
          </el-form-item>
          <el-form-item label="物流商" :prop="['data', i, 'tracking_supplier']" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
            <VSelect v-model="d.tracking_supplier" placeholder="请选择" :options="logisticsOptions" class="w-full" @change="val => logisticsChange(val.option, d)" />
          </el-form-item>
          <el-form-item label="SKU">
            <div class="flex gap-1 w-full">
              <VSelect v-model="d.skus" placeholder="全选" :options="form.sku" multiple collapse-tags class="w-full" />
              <el-button v-if="!i" type="primary" link @click="form.data.push({ skus: [], tracking_no: '', tracking_supplier: '', logistics_code: '' })">添加</el-button>
              <el-button v-else type="danger" link @click="form.data.splice(i, 1)">删除</el-button>
            </div>
          </el-form-item>
        </template>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()

const emit = defineEmits(['close', 'success'])

const logisticsChange = (option, d) => {
  d.logistics_code = option.code
}

const getSupplier = (tracking_no, d) => {
  api.getSupplier({ tracking_no }).then(res => {
    Object.assign(d, res)
  })
}

// 打开弹窗
const logisticsOptions = ref([])
const open = async (params) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  api.getLogistics().then(res => {
    logisticsOptions.value = res.map(item => ({
      ...item,
      code: item.value,
      value: item.label,
    }));
  })
  loadings = reactive({})
  form.value = { ...params, data: [{ skus: [], tracking_no: '', tracking_supplier: '', logistics_code: '' }] }
  formRef?.value?.resetFields()
  visible.value = true
  const data = await api.getTrack(params)
  if (data?.length) {
    form.value.data = data
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  if (!await formRef?.value.validate()) return
  loadings.submit = true
  api.addTrack(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>